package com.shbaiche.bifenba.ui.home.sub;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.kit.AppKit;
import com.shbaiche.bifenba.model.Event;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.video.ArticleCollectMessage;
import com.shbaiche.bifenba.model.video.ArticleDetailMessage;
import com.shbaiche.bifenba.model.video.AuthorMessage;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.GanhuoFragment;
import com.shbaiche.bifenba.ui.GirlFragment;
import com.shbaiche.bifenba.ui.HomeFragment;
import com.shbaiche.bifenba.ui.my.AboutActivity;
import com.shbaiche.bifenba.ui.my.LoginActivity;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroid.base.XActivity;
import cn.droidlover.xdroidbase.base.XFragmentAdapter;
import cn.droidlover.xdroidbase.kit.Kits;
import cn.droidlover.xdroidbase.router.Router;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import okhttp3.Call;


public class SubActivity extends XActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.home_sub_head)
    ImageView head;
    @BindView(R.id.home_sub_tv_name)
    TextView name;
    @BindView(R.id.home_sub_tv_fans)
    TextView fans;

    List<Fragment> fragmentList = new ArrayList<>();
    String[] titles = {"资讯", "视频"};

    XFragmentAdapter adapter;

    private Dialog dg;

    private Long id;


    @Override
    public void initData(Bundle savedInstanceState) {
        setSupportActionBar(toolbar);

        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            //让应用主题内容占用系统状态栏的空间,注意:下面两个参数必须一起使用 stable 牢固的
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //设置状态栏颜色为透明
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        findViewById(R.id.top_head).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        id = getIntent().getLongExtra("id", 0l);
        dg = new Dialog(this);
        dg.setContentView(R.layout.view_loading);
        load();
    }

    private void load() {
        dg.show();

        AuthorMessage msg = new AuthorMessage();
        msg.setAuthorId(id);
        NetApi.AuthorDetail(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                dg.hide();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
                dg.hide();
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                dg.hide();
                Gson gson = new Gson();
                AuthorMessage msg = gson.fromJson(response.getMessage(), AuthorMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {

                    name.setText(msg.getName() + "");
                    long fa = msg.getFans() == null ? 0 : msg.getFans();
                    fans.setText("粉丝：" + fa);
                    Glide.with(SubActivity.this).load(msg.getPhoto()).placeholder(R.drawable.defult).error(R.drawable.defult).into(head);

                    fragmentList.clear();
                    fragmentList.add(new SubInfoFragment(msg.getArticleList(), 1));
                    fragmentList.add(new SubInfoFragment(msg.getVedioList(), 2));

                    if (adapter == null) {
                        adapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
                        viewPager.setAdapter(adapter);
                        viewPager.setOffscreenPageLimit(2);

                        tabLayout.setupWithViewPager(viewPager);

//        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

                        setTabLine(tabLayout);
                    }
                } else {
                }
            }
        });
    }

    @Override
    public void setListener() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event.TestEvent event) {
        if (event != null && !Kits.Empty.check(event.name)) {
            toolbar.setTitle(event.name);
        }
    }

    public static void setTabLine(final TabLayout tabLayout) {
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    Field mTabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
                    mTabStripField.setAccessible(true);
                    LinearLayout mTabStrip = (LinearLayout) mTabStripField.get(tabLayout);
                    int dp10 = AppKit.dp2px(tabLayout.getContext(), 20);
                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);
                        //拿到tabView的mTextView属性
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);
                        TextView mTextView = (TextView) mTextViewField.get(tabView);
                        tabView.setPadding(0, 0, 0, 0);
                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);
                        tabView.invalidate();
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    @Override
    public boolean useEventBus() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_home_sub;
    }

    public static void launch(Activity activity, Bundle b) {
        Router.newIntent(activity)
                .to(SubActivity.class)
                .data(b)
                .launch();
    }

    @Override
    protected void onDestroy() {
        if (dg != null) {
            dg.dismiss();
            dg = null;
        }
        super.onDestroy();
    }

}
